<?php

namespace YouBaoLian\Application\Callback;

class CallbackRequest
{
    private $body;

    private $bizContent;

    private $sign;

    private $success = false;

    private $error;

    public function build(string $body)
    {
        if (!json_decode($body)) {
            $this->setError('错误数据来源');
            return $this;
        }
        $this->success = true;

        $this->body = json_decode($body, JSON_UNESCAPED_UNICODE|JSON_UNESCAPED_SLASHES);

        $this->bizContent = $this->body['bizContent'] ?? [];

        $this->sign = $this->body['sign'] ?? '';
    }

    public function construe(): CallbackRequest
    {
        $this->bizContent=  $this->getBizContent('bizContent');
        return $this;
    }


    public function setError($error)
    {
        $this->error = $error;
        $this->success = false;
    }

    protected function getBizContent($key = '', $default = '')
    {
        if ($key) {
            return $this->bizContent[$key] ?? $default;
        }
        return $this->bizContent;
    }

    public function setBizContent($bizContent = [])
    {
        $this->bizContent = $bizContent;
    }

    public function isSuccess(): bool
    {
        return $this->success;
    }

    /**
     * @return mixed
     */
    public function getBody()
    {
        return $this->body;
    }

    /**
     * @return mixed
     */
    public function getSign()
    {
        return $this->sign;
    }

    /**
     * @return mixed
     */
    public function getError()
    {
        return $this->error;
    }
}